
package com.zjff.myshop.admin.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.collection.CollectionUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zjff.myshop.admin.pojo.form.BatchIdsForm;
import com.zjff.myshop.admin.pojo.form.GoodsInsertForm;
import com.zjff.myshop.admin.pojo.form.GoodsUpdateForm;
import com.zjff.myshop.front.controller.GoodsController;
import com.zjff.myshop.entity.Goods;
import com.zjff.myshop.exception.BusinessException;

import com.zjff.myshop.util.BeanUtils;
import com.zjff.myshop.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@CrossOrigin
@RestController
@Api(value = "v1", tags = "83.后台管理系统-商品模块")
@SaCheckRole("admin")
@RequestMapping("/admin/v1/goods")
public class AdminGoodsController extends GoodsController {
    private static final Logger logger = LoggerFactory.getLogger(AdminGoodsController.class);

    /**
     * 增加
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增商品", notes = "新增商品信息")
    @ApiImplicitParam(name = "form", value = "新增商品参数", dataType = "GoodsInsertForm", required = true)
    @PostMapping(value = "/insert")
    public Result insert(@RequestBody @Valid GoodsInsertForm form) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(form, goods);

        //转换商品轮播图数据格式
        List<String> albumList = form.getAlbumList();
        if (CollectionUtil.isNotEmpty(albumList)) {
            String albums = CollectionUtil.join(albumList, "|"); // 以竖线为分隔符
            goods.setAlbum(albums);
        } else {
            goods.setAlbum("");
        }

        if (goodsService.insert(goods)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 删除（单个）
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "删除单条商品", notes = "删除商品单个记录")
    @DeleteMapping(value = "/{id}")
    public Result deleteById(@PathVariable Integer id) {
        if (goodsService.deleteById(id)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 删除（批量）
     */
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "批量删除商品", notes = "批量删除商品信息")
    @ApiImplicitParam(name = "form", value = "批量删除参数", dataType = "BatchIdsForm", required = true)
    @DeleteMapping(value = "/batch")
    public Result deleteBatch(@Valid @RequestBody BatchIdsForm form) {
        if (form == null || form.getIds().length < 1) {
            throw new BusinessException("数据不存在！");
        }

        if (goodsService.deleteBatch(form.getIds())) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 更改
     */
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "更改商品", notes = "更改商品信息")
    @ApiImplicitParam(name = "form", value = "更改商品参数", dataType = "GoodsUpdateForm", required = true)
    @PostMapping(value = "/update")
    public Result update(@RequestBody @Valid GoodsUpdateForm form) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(form, goods);

        //转换商品轮播图数据格式
        List<String> albumList = form.getAlbumList();
        if (CollectionUtil.isNotEmpty(albumList)) {
            String albums = CollectionUtil.join(albumList, "|"); // 以竖线为分隔符
            goods.setAlbum(albums);
        } else {
            goods.setAlbum("");
        }

        if (goodsService.update(goods)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

}